package com.linkhub.oms.dao.mpservice.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.linkhub.oms.common.enums.mp.ApprovalStatusEnum;
import com.linkhub.oms.common.enums.mp.ProcessStatusEnum;
import com.linkhub.oms.dao.mapper.WhiteListApprovalRecordEntityMapper;
import com.linkhub.oms.dao.mpservice.IWhiteListApprovalRecordService;
import com.linkhub.oms.entity.WhiteListApprovalRecordEntity;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 白名单审批表 服务实现类
 * </p>
 *
 * @author echo
 * @since 2024-03-05
 */
@Service
public class WhiteListApprovalRecordServiceImpl extends ServiceImpl<WhiteListApprovalRecordEntityMapper, WhiteListApprovalRecordEntity> implements IWhiteListApprovalRecordService {

    @Override
    public boolean exists(String email) {
        return super.lambdaQuery()
                .eq(WhiteListApprovalRecordEntity::getEmail, email)
                .exists();
    }

    @Override
    public WhiteListApprovalRecordEntity getPendingApprovalRecord(String email) {

        return lambdaQuery()
                .eq(WhiteListApprovalRecordEntity::getEmail, email)
                .eq(WhiteListApprovalRecordEntity::getApprovalStatus, ApprovalStatusEnum.PENDING_APPROVAL)
                .eq(WhiteListApprovalRecordEntity::getProcessStatus, ProcessStatusEnum.PENDING)
                .one();
    }

    @Override
    public WhiteListApprovalRecordEntity getPendingApprovalRecord(Long id) {
        return lambdaQuery()
                .eq(WhiteListApprovalRecordEntity::getId, id)
                .eq(WhiteListApprovalRecordEntity::getApprovalStatus, ApprovalStatusEnum.PENDING_APPROVAL)
                .eq(WhiteListApprovalRecordEntity::getProcessStatus, ProcessStatusEnum.PENDING)
                .one();
    }

    @Override
    public List<WhiteListApprovalRecordEntity> listPendingApprovalRecords(Collection<String> emails) {
        if (CollUtil.isEmpty(emails)) {
            return Lists.newArrayListWithCapacity(0);
        }
        return lambdaQuery()
                .in(WhiteListApprovalRecordEntity::getEmail, emails)
                .eq(WhiteListApprovalRecordEntity::getApprovalStatus, ApprovalStatusEnum.PENDING_APPROVAL)
                .eq(WhiteListApprovalRecordEntity::getProcessStatus, ProcessStatusEnum.PENDING)
                .list();
    }

}
